package com.sharein.auth.actions;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sharein.auth.business.UserManager;

public class StartPage implements Action{

	@Override
	public boolean execute(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Cookie[] cookies = request.getCookies();
		String shareinID = null;
		if(cookies != null) {
			for (Cookie cookie : cookies) {
				if(cookie.getName().equals("shareinID")) shareinID = cookie.getValue();
			}
		}
		if(shareinID == null) {
			HttpSession session = request.getSession();
			Object shareinIDObj = session.getAttribute("shareinID");
			if(shareinIDObj != null) response.sendRedirect("home.html");
			else response.sendRedirect("authentification.html");
		}
		else {
			UserManager.setUserSignedIn(shareinID, true);
			response.sendRedirect("home.html");
		}
		return true;
	}

}
